#include"process.h"
#include<string.h> //用man可以查头文件
#include<unistd.h>


#define LENGTH 101 //缓冲区的大小
#define LABLE '#'

void FlushProcess(double target, double current)
{
  //step 0:定义光标
  const static char *sym = "|/-\\";
  static int index = 0;
  int len = strlen(sym);

  //step 1:更新出进度
  double rate = (current / target)*100.0;//5.0 100.0 -> 0.05 -> 5.0
  if(rate>100) rate=100;
  //step 2:整数个进度递增，刷新一个#
  int cnt = (int)rate;
  //step 3:构建缓冲区
  char bar[LENGTH];
  memset(bar, 0, sizeof(bar));
  for(int i = 0; i < cnt; i++)
    bar[i] = LABLE;
  //step 4:刷新
  printf("[%-100s][%.1lf%%][%c]\r", bar, rate, sym[index++]);
  index %= len;
  fflush(stdout);

  //step 5:处理更新到100%
  if(rate >= 100.0)
    printf("\n");
}

void Process()
{
  const char *symb = "|/-\\";
  int s_len = strlen(symb);
  char bar[LENGTH];
  memset(bar, '\0', sizeof(bar));
  int cnt = 0;
  while(cnt<=100)
  {
    printf("[%-100s][%d%%][%c]\r", bar, cnt, symb[cnt%s_len]);
    fflush(stdout);
    bar[cnt++] = LABLE;
    usleep(20000);
  }
  printf("\n");
}

